package com.finals.function;

import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.finals.function.util.Util;

/**
 * Type<br>
 * 1表示下一首 <br>
 * 2表示上一首 <br>
 * 
 * @author liufeilong
 * 
 */
public class FunctionActivity extends BaseActivity {

	Handler mHandler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(FunctionActivity.this, "调用下一曲失败", Toast.LENGTH_SHORT).show();
				FunctionActivity.this.finish();
				break;
			case 1:
				FunctionActivity.this.finish();
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		InitWindow();
		InitData();
	}

	private void InitWindow() {
		Window mWindow = getWindow();
		mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
		mWindow.setType(Util.getWindowType(mApp.getBaseConfig().getWindowMode()));
	}

	private void InitData() {
		InitIntent(getIntent());
	}

	private void InitIntent(Intent intent) {
		int Type = intent.getIntExtra("Type", -1);
		switch (Type) {
		case 0:
			this.finish();
			break;
		case 1:
			NextSong();
			break;
		case 2:
			ForwardSong();
			break;
		case 3:
			PlaySong();
			break;
		default:
			this.finish();
			break;
		}
	}

	private void PlaySong() {
		new Thread(playsongRunnable).start();
	}

	Runnable playsongRunnable = new Runnable() {

		@Override
		public void run() {
			Instrumentation instrumentation = new Instrumentation();
			try {
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
			} catch (Exception e) {
				e.printStackTrace();
				mHandler.sendEmptyMessage(0);
			} finally {
				mHandler.sendEmptyMessage(1);
			}
		}
	};

	private void NextSong() {
		new Thread(mNextRunnable).start();
	}

	Runnable mNextRunnable = new Runnable() {

		@Override
		public void run() {
			Instrumentation instrumentation = new Instrumentation();
			try {
				Thread.sleep(200);
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_NEXT);
			} catch (Exception e) {
				e.printStackTrace();
				mHandler.sendEmptyMessage(0);
			} finally {
				mHandler.sendEmptyMessage(1);
			}
		}
	};

	private void ForwardSong() {
		new Thread(mForwardRunnable).start();
	}

	Runnable mForwardRunnable = new Runnable() {

		@Override
		public void run() {
			Instrumentation instrumentation = new Instrumentation();
			try {
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_0);
				instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_MEDIA_PREVIOUS);
			} catch (Exception e) {
				e.printStackTrace();
				mHandler.sendEmptyMessage(0);
			} finally {
				mHandler.sendEmptyMessage(1);
			}
		}
	};

	@Override
	protected void onDestroy() {
		mHandler.removeCallbacksAndMessages(null);
		super.onDestroy();
	}
}
